#include <mono/common/date_time_formatter.h>
#include <mono/common/date_time_format.h>
#include <mono/common/timestamp.h>
#include <mono/common/number_formatter.h>
namespace mono {


void DateTimeFormatter::append(std::string& str, const LocalDateTime& dateTime, const std::string& fmt)
{
	DateTimeFormatter::append(str, dateTime._dateTime, fmt, dateTime.tzd());
}


void DateTimeFormatter::append(std::string& str, const DateTime& dateTime, const std::string& fmt, int timeZoneDifferential)
{
	std::string::const_iterator it  = fmt.begin();
	std::string::const_iterator end = fmt.end();
	std::string word;
	while (it != end) {
		if (*it == '%')  {
			if (++it != end) {
				switch (*it) {
					case 'w': str.append(DateTimeFormat::WEEKDAY_SHORT_NAMES[dateTime.dayOfWeek()]); break;
					case 'W': str.append(DateTimeFormat::WEEKDAY_NAMES[dateTime.dayOfWeek()]); break;
					case 'b': str.append(DateTimeFormat::MONTH_SHORT_NAMES[dateTime.month() - 1]); break;
					case 'B': str.append(DateTimeFormat::MONTH_NAMES[dateTime.month() - 1]); break;
					case 'd': NumberFormatter::append0(str, dateTime.day(), 2); break;
					case 'e': NumberFormatter::append(str, dateTime.day()); break;
					case 'f': NumberFormatter::append0(str, dateTime.day(), 2); break;
					case 'm': NumberFormatter::append0(str, dateTime.month(), 2); break;
					case 'n': NumberFormatter::append(str, dateTime.month()); break;
					case 'o': NumberFormatter::append0(str, dateTime.month(), 2); break;
					case 'y': NumberFormatter::append0(str, dateTime.year() % 100, 2); break;
					case 'Y': NumberFormatter::append0(str, dateTime.year(), 4); break;
					case 'H': NumberFormatter::append0(str, dateTime.hour(), 2); break;
					case 'h': NumberFormatter::append0(str, dateTime.hourAMPM(), 2); break;
					case 'a': str.append(dateTime.isAM() ? "am" : "pm"); break;
					case 'A': str.append(dateTime.isAM() ? "AM" : "PM"); break;
					case 'M': NumberFormatter::append0(str, dateTime.minute(), 2); break;
					case 'S': NumberFormatter::append0(str, dateTime.second(), 2); break;
					case 's': NumberFormatter::append0(str, dateTime.second(), 2); 
							  NumberFormatter::append0(str,dateTime.millisecond()*1000 + dateTime.microsecond(), 6); 
							  break;
					case 'i': NumberFormatter::append0(str, dateTime.millisecond() ,3); break;
					case 'c': NumberFormatter::append(str, dateTime.millisecond()/100); break;
					case 'F': NumberFormatter::append0(str, dateTime.millisecond()*1000 + dateTime.microsecond(), 6); break;
					case 'z': tzdISO(str, timeZoneDifferential);break;
					case 'Z': tzdRFC(str, timeZoneDifferential);break;
					default:  str.append(1, *it);
				}
				++it;
			}
		} else {
			str.append(1, *it++);
		}
	}
}


void DateTimeFormatter::append(std::string& str, const Timespan& timespan, const std::string& fmt)
{
	std::string::const_iterator it  = fmt.begin();
	std::string::const_iterator end = fmt.end();

	while (it != end) {
		if (*it == '%') {
			if (++it != end) {
				switch (*it) {
				case 'd': NumberFormatter::append(str, timespan.days()); break;
				case 'H': NumberFormatter::append0(str, timespan.hours(), 2); break;
				case 'h': NumberFormatter::append(str, timespan.totalHours()); break;
				case 'M': NumberFormatter::append0(str, timespan.minutes(), 2); break;
				case 'm': NumberFormatter::append(str, timespan.totalMinutes()); break;
				case 'S': NumberFormatter::append(str, timespan.seconds()); break;
				case 's': NumberFormatter::append(str, timespan.totalSeconds()); break;
				case 'i': NumberFormatter::append0(str, timespan.milliseconds(), 3); break;
				case 'c': NumberFormatter::append(str, timespan.milliseconds()/100); break;
				case 'F': NumberFormatter::append0(str, timespan.milliseconds()*1000 + timespan.microseconds(), 6); break;
				default:  str.append(1, *it);
				}
				++it;
			}
		}
		else str.append(1, *it++);
	}
}


void DateTimeFormatter::tzdISO(std::string& str, int timeZoneDifferential)
{
	if (timeZoneDifferential != UTC) {
		if (timeZoneDifferential >= 0) {
			str.append(1, '+');
			 NumberFormatter::append0(str, timeZoneDifferential/3600, 2);
			 str.append(1, ':');
			 NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2);
		} else {
			str.append(1, '-');
			NumberFormatter::append0(str, -timeZoneDifferential/3600, 2);
			str.append(1, ':');
			NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2);
		}
	} else {
		str.append(1, 'Z');
	}
}


void DateTimeFormatter::tzdRFC(std::string& str,  int timeZoneDifferential)
{
	if (timeZoneDifferential != UTC) {
		if (timeZoneDifferential >= 0) {
			str.append(1, '+');
			NumberFormatter::append0(str, timeZoneDifferential/3600, 2);
			str.append(1, ':');
			NumberFormatter::append0(str, (timeZoneDifferential%3600)/60, 2);
		} else {
			str.append(1, '-');
			NumberFormatter::append0(str, -timeZoneDifferential/3600, 2);
			str.append(1, ':');
			NumberFormatter::append0(str, (-timeZoneDifferential%3600)/60, 2);
		}		
	} else {
		str.append("GMT");
	}
}


} // namespace Poco
